package com.tianbing.adlottery.controller.websiteadmin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jubao.ejb.business.WebsiteBusiness;
import com.jubao.ejb.business.util.BusinessConstant;
import com.jubao.ejb.dao.CategoryDao;
import com.jubao.ejb.dao.WebsiteDao;
import com.jubao.ejb.entity.Category;
import com.jubao.ejb.entity.Website;
import com.tianbing.adlottery.util.ControllerFormBean;

public class UpdateWebsitePrimaryCategoryController extends SimpleFormController {
	static Logger logger = Logger.getLogger(UpdateWebsitePrimaryCategoryController.class.getName());

	private CategoryDao categoryDao;

	private WebsiteDao websiteDao;

	private WebsiteBusiness websiteBusiness;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		ControllerFormBean fb = new ControllerFormBean();
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {
		long childCatCounts = 0;
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		Map model = mv.getModel();

		long wbsid = ServletRequestUtils.getLongParameter(request, "wbsid", -1);
		Website website = websiteDao.find(wbsid);
		String type = ServletRequestUtils.getStringParameter(request, "type", "");
		String catid = ServletRequestUtils.getStringParameter(request, "catid", "");
		Category cat = categoryDao.find(catid);
		if (website != null && cat != null) {
			childCatCounts = categoryDao.getChildCategoryCount(cat.getSysId());
			if (childCatCounts == 0) {//only the category is leaf node
				if (type.equals("area")) {
					Category oldCat = website.getPrimaryAreaCategory();
					if (oldCat != null) {
						websiteBusiness.removeFromCategoryTrail(website.getSysId(), oldCat.getSysId());
					}
					website.setPrimaryAreaCategory(cat);
					websiteDao.save(website);
					websiteBusiness.add2CateogryTrail(website.getSysId(), cat.getSysId());
				} else if (type.equals("content")) {
					Category oldCat = website.getPrimaryWebsiteCategory();
					if (oldCat != null) {
						websiteBusiness.removeFromCategoryTrail(website.getSysId(), oldCat.getSysId());
					}
					website.setPrimaryWebsiteCategory(cat);
					websiteDao.save(website);
					websiteBusiness.add2CateogryTrail(website.getSysId(), cat.getSysId());
				}
			}
		}
		model.put("website", website);
		model.put("wbsid", wbsid);
		model.put("type", type);
		model.put("catid", catid);
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();
		long wbsid = ServletRequestUtils.getLongParameter(request, "wbsid", -1);
		String catid = ServletRequestUtils.getStringParameter(request, "catid", "");
		String type = ServletRequestUtils.getStringParameter(request, "type", "area");
		Website website = websiteDao.find(wbsid);
		if (website != null) {
			model.put("type", type);
			if (type.equals("area") && catid == "") {
				catid = BusinessConstant.CategoryID.AREA_CATEGORY_TOP.getId();
			} else if (type.equals("content") && catid == "") {
				catid = BusinessConstant.CategoryID.WEBSITE_CATEGORY_TOP.getId();
			}

			if (!catid.equals("")) {
				Category cat = categoryDao.find(catid);

				if (cat != null) {
					model.put("category", cat);
					List primaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(cat.getSysId());
					List<Category> childCategoryList = categoryDao.getChildCategoryList(cat.getSysId(), 0, 100, null);
					model.put("primaryCategoryTrail", primaryCategoryTrail);
					model.put("childCategoryList", childCategoryList);
				}
			}

			Category areaCat = website.getPrimaryAreaCategory();
			if (areaCat != null) {
				List<Category> primaryAreaCategoryTrail = categoryDao.getPrimaryCategoryTrail(areaCat.getSysId());
				model.put("primaryAreaCategoryTrail", primaryAreaCategoryTrail);
			}

			Category websiteCat = website.getPrimaryWebsiteCategory();
			if (websiteCat != null) {
				List<Category> websitePrimaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(websiteCat.getSysId());
				model.put("websitePrimaryCategoryTrail", websitePrimaryCategoryTrail);
			}

			model.put("website", website);
		}
		return model;
	}

	/**
	 * @param logger the logger to set
	 */
	public static void setLogger(Logger logger) {
		UpdateWebsitePrimaryCategoryController.logger = logger;
	}

	/**
	 * @param categoryDao the categoryDao to set
	 */
	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	/**
	 * @param websiteDao the websiteDao to set
	 */
	public void setWebsiteDao(WebsiteDao websiteDao) {
		this.websiteDao = websiteDao;
	}

	public void setWebsiteBusiness(WebsiteBusiness websiteBusiness) {
		this.websiteBusiness = websiteBusiness;
	}

}
